# -*- coding: utf-8 -*-
# standard
import os
import sys

# third

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from functions import *


"""
@Title:   找出下载遗漏的文件  此脚本只能在hk02上运行
@File: 比对已下载文件和数据库记录.py
@Author: walle 2023年11日21日 14时00分01秒
@Version: 1.0.0
@Desc: 
"""


def list_files():
    names = []
    dir_path = "/mnt/projects/LRS3"
    for folder in os.listdir(dir_path):
        folder_path = os.path.join(dir_path, folder)
        for name in os.listdir(folder_path):
            names.append(name)
    return names

def main():
    """
    统计指定文件夹的而信息
    :param dir_path: 文件夹路径
    :return: 统计结果
    """
    exist_files = list_files()
    db_session = new_db_session()
    cond = and_(
        MediaFileOrm.error == "",
    )
    files = [f"{x.key}.mp4" for x in tqdm(db_session.query(MediaFileOrm).filter(cond).order_by(MediaFileOrm.key.desc()).all())]
    db_session.close()
    for x in files:
        if x not in exist_files:
            print(f"数据库标记已下载但磁盘上不存在的文件 {x}")
    for x in exist_files:
        if x not in files:
            print(f"磁盘上存在但数据库没有标记的文件 {x}")



if __name__ == '__main__':
    main()
    pass